{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Linear regression_MicroPython.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "**Linear Regression model development for MicroPython**"
      ],
      "metadata": {
        "id": "n9LqWZkVLMwN"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "*First, perform test using scikit-learn*"
      ],
      "metadata": {
        "id": "snFA-lFOLXDA"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import numpy as np\n"
      ],
      "metadata": {
        "id": "T48-dx10MLwZ"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "X=np.array([1,2,3,4,5,6,7,8,9,10])\n",
        "y=np.array([12, 23, 31, 45, 55, 61, 76, 80, 95, 104])"
      ],
      "metadata": {
        "id": "c3ObZ9VrLmrI"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "X=X.reshape(-1,1)\n",
        "y=y.reshape(-1,1)"
      ],
      "metadata": {
        "id": "wt5GI5RDL5YL"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from sklearn.linear_model import LinearRegression\n",
        "lm = LinearRegression()\n",
        "lm.fit(X,y)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "dw7w_0K0LVHo",
        "outputId": "29023377-d63c-4d37-b411-3aa55d663b2a"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "LinearRegression()"
            ]
          },
          "metadata": {},
          "execution_count": 7
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "*Lets Predict values*"
      ],
      "metadata": {
        "id": "HKDBMF9EMYV9"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "Xtest=np.array([2.5, 7.1, 5.78, 3.54, 8.02])\n",
        "Xtest=Xtest.reshape(-1,1)"
      ],
      "metadata": {
        "id": "lyjyelyEMchr"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "pred=lm.predict(Xtest)\n",
        "print(pred)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "9K1_0tPiMXtQ",
        "outputId": "162941bd-93d8-4288-c520-73499a0cabc8"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[[27.72727273]\n",
            " [74.45212121]\n",
            " [61.04412121]\n",
            " [38.29115152]\n",
            " [83.79709091]]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Develop the same in MicroPython environment without built model**"
      ],
      "metadata": {
        "id": "Vj_70LZgM4SN"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "*Different Functions used in the Linear Regression*"
      ],
      "metadata": {
        "id": "Fs_zYtWTNMKJ"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def add(x, y):\n",
        "    if len(x) != len(y):\n",
        "        print(\"Dimention mismatch\")\n",
        "        exit()\n",
        "    else:\n",
        "        z = [x[i] + y[i] for i in range(len(x))]\n",
        "    return z\n",
        "\n",
        "\n",
        "def sub(x, y):\n",
        "    if len(x) != len(y):\n",
        "        print(\"Dimention mismatch\")\n",
        "        exit()\n",
        "    else:\n",
        "        z = [x[i] - y[i] for i in range(len(x))]\n",
        "    return z\n",
        "\n",
        "\n",
        "def mul(x, y):\n",
        "    z = [x[i] * y[i] for i in range(len(x))]\n",
        "    return z\n",
        "\n",
        "\n",
        "def div(x, y):\n",
        "    if len(x) != len(y):\n",
        "        print(\"Dimention mismatch\")\n",
        "        exit()\n",
        "    else:\n",
        "        z = [x[i] / y[i] for i in range(len(x))]\n",
        "    return z\n",
        "\n",
        "\n",
        "def pypow(x, y):\n",
        "    z = [x[i] ** y for i in range(len(x))]\n",
        "    return z\n",
        "\n",
        "\n",
        "def ones1d(n):\n",
        "    z = []\n",
        "    for i in range(n):\n",
        "        z.append(1)\n",
        "    return z\n",
        "\n",
        "\n",
        "def expand(val, n):\n",
        "    z = []\n",
        "    for i in range(n):\n",
        "        z.append(val)\n",
        "    return z\n",
        "\n",
        "\n",
        "def random1d(strt, end, n):\n",
        "    z = []\n",
        "    import random\n",
        "    for i in range(n):\n",
        "        val = random.randint(strt, end)\n",
        "        z.append(val)\n",
        "    return z\n",
        "\n",
        "\n",
        "def linear_regressor(x, y, lr, niter):  # x, y both are row vector\n",
        "    N = len(x)\n",
        "    # W = random1d(1, 20, N)\n",
        "    import random\n",
        "    W = expand(random.randint(1, 20), N)\n",
        "    b = []\n",
        "    for i in range(N):\n",
        "        b.append(0)\n",
        "\n",
        "    for i in range(niter):\n",
        "        ypred = add(mul(W, x), b)\n",
        "        #Gradient-Descent Algorithm\n",
        "        L = mul(div(ones1d(N), expand(N, N)), expand(sum(pypow(sub(y, ypred), 2)), N))\n",
        "        # print(L)\n",
        "        dL_dW = mul(div(expand(-2, N), expand(N, N)), expand(sum(mul(sub(y, ypred), x)), N))\n",
        "        dL_db = mul(div(expand(-2, N), expand(N, N)), expand(sum(sub(y, ypred)), N))\n",
        "        # update weight\n",
        "        W = sub(W, mul(expand(lr, N), dL_dW))\n",
        "        # print(W)\n",
        "        b = sub(b, mul(expand(lr, N), dL_db))\n",
        "        print(\"MSE Loss is:\" + str(L[0]))\n",
        "        print(\"Iteration:\" + str(i))\n",
        "    # store result\n",
        "    global weight, bias\n",
        "    weight = W[0]\n",
        "    bias = b[0]\n",
        "    return ypred\n",
        "\n",
        "\n",
        "def linear_pred(x):  # prediction function\n",
        "    y = add(mul(expand(weight, len(x)), x), expand(bias, len(x)))\n",
        "    return y"
      ],
      "metadata": {
        "id": "Kr7TDENWM_8a"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "#Main function\n",
        "x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
        "y = [12, 23, 31, 45, 55, 61, 76, 80, 95, 104]\n",
        "#Fit the model\n",
        "ypred = linear_regressor(x, y, 0.005, 100)\n",
        "print(ypred)\n",
        "# z=div(x,y)\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "LgNrGMc5NS-s",
        "outputId": "030d567a-0f54-47b4-964f-3f8eaaef951a"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "MSE Loss is:2165.3\n",
            "Iteration:0\n",
            "MSE Loss is:800.6558529999999\n",
            "Iteration:1\n",
            "MSE Loss is:297.6878595904124\n",
            "Iteration:2\n",
            "MSE Loss is:112.30796231854661\n",
            "Iteration:3\n",
            "MSE Loss is:43.98155489800109\n",
            "Iteration:4\n",
            "MSE Loss is:18.797571329172793\n",
            "Iteration:5\n",
            "MSE Loss is:9.514600364426531\n",
            "Iteration:6\n",
            "MSE Loss is:6.092271492627629\n",
            "Iteration:7\n",
            "MSE Loss is:4.830004029525936\n",
            "Iteration:8\n",
            "MSE Loss is:4.3638744539756145\n",
            "Iteration:9\n",
            "MSE Loss is:4.1911812801417945\n",
            "Iteration:10\n",
            "MSE Loss is:4.1266437400913\n",
            "Iteration:11\n",
            "MSE Loss is:4.10197286133751\n",
            "Iteration:12\n",
            "MSE Loss is:4.091999352760005\n",
            "Iteration:13\n",
            "MSE Loss is:4.087446549886756\n",
            "Iteration:14\n",
            "MSE Loss is:4.08489533578681\n",
            "Iteration:15\n",
            "MSE Loss is:4.0830855106551\n",
            "Iteration:16\n",
            "MSE Loss is:4.08155258711785\n",
            "Iteration:17\n",
            "MSE Loss is:4.080125353700293\n",
            "Iteration:18\n",
            "MSE Loss is:4.078740691728204\n",
            "Iteration:19\n",
            "MSE Loss is:4.0773753223688685\n",
            "Iteration:20\n",
            "MSE Loss is:4.076020650628816\n",
            "Iteration:21\n",
            "MSE Loss is:4.0746734936024875\n",
            "Iteration:22\n",
            "MSE Loss is:4.073332663181437\n",
            "Iteration:23\n",
            "MSE Loss is:4.0719977065426844\n",
            "Iteration:24\n",
            "MSE Loss is:4.070668441930128\n",
            "Iteration:25\n",
            "MSE Loss is:4.069344787557133\n",
            "Iteration:26\n",
            "MSE Loss is:4.068026698544929\n",
            "Iteration:27\n",
            "MSE Loss is:4.06671414367966\n",
            "Iteration:28\n",
            "MSE Loss is:4.0654070968455365\n",
            "Iteration:29\n",
            "MSE Loss is:4.064105533867037\n",
            "Iteration:30\n",
            "MSE Loss is:4.0628094313448155\n",
            "Iteration:31\n",
            "MSE Loss is:4.061518766226397\n",
            "Iteration:32\n",
            "MSE Loss is:4.06023351564764\n",
            "Iteration:33\n",
            "MSE Loss is:4.05895365687414\n",
            "Iteration:34\n",
            "MSE Loss is:4.0576791672792885\n",
            "Iteration:35\n",
            "MSE Loss is:4.05641002433602\n",
            "Iteration:36\n",
            "MSE Loss is:4.055146205613433\n",
            "Iteration:37\n",
            "MSE Loss is:4.0538876887753705\n",
            "Iteration:38\n",
            "MSE Loss is:4.052634451579603\n",
            "Iteration:39\n",
            "MSE Loss is:4.051386471877283\n",
            "Iteration:40\n",
            "MSE Loss is:4.050143727612533\n",
            "Iteration:41\n",
            "MSE Loss is:4.048906196821998\n",
            "Iteration:42\n",
            "MSE Loss is:4.047673857634474\n",
            "Iteration:43\n",
            "MSE Loss is:4.046446688270506\n",
            "Iteration:44\n",
            "MSE Loss is:4.045224667042009\n",
            "Iteration:45\n",
            "MSE Loss is:4.044007772351884\n",
            "Iteration:46\n",
            "MSE Loss is:4.042795982693638\n",
            "Iteration:47\n",
            "MSE Loss is:4.041589276650994\n",
            "Iteration:48\n",
            "MSE Loss is:4.040387632897519\n",
            "Iteration:49\n",
            "MSE Loss is:4.039191030196263\n",
            "Iteration:50\n",
            "MSE Loss is:4.037999447399328\n",
            "Iteration:51\n",
            "MSE Loss is:4.036812863447592\n",
            "Iteration:52\n",
            "MSE Loss is:4.035631257370226\n",
            "Iteration:53\n",
            "MSE Loss is:4.034454608284408\n",
            "Iteration:54\n",
            "MSE Loss is:4.033282895394904\n",
            "Iteration:55\n",
            "MSE Loss is:4.032116097993732\n",
            "Iteration:56\n",
            "MSE Loss is:4.030954195459781\n",
            "Iteration:57\n",
            "MSE Loss is:4.0297971672584385\n",
            "Iteration:58\n",
            "MSE Loss is:4.028644992941252\n",
            "Iteration:59\n",
            "MSE Loss is:4.02749765214554\n",
            "Iteration:60\n",
            "MSE Loss is:4.026355124594047\n",
            "Iteration:61\n",
            "MSE Loss is:4.025217390094583\n",
            "Iteration:62\n",
            "MSE Loss is:4.024084428539688\n",
            "Iteration:63\n",
            "MSE Loss is:4.022956219906211\n",
            "Iteration:64\n",
            "MSE Loss is:4.021832744255046\n",
            "Iteration:65\n",
            "MSE Loss is:4.020713981730705\n",
            "Iteration:66\n",
            "MSE Loss is:4.019599912561022\n",
            "Iteration:67\n",
            "MSE Loss is:4.018490517056738\n",
            "Iteration:68\n",
            "MSE Loss is:4.017385775611236\n",
            "Iteration:69\n",
            "MSE Loss is:4.016285668700134\n",
            "Iteration:70\n",
            "MSE Loss is:4.015190176880941\n",
            "Iteration:71\n",
            "MSE Loss is:4.014099280792749\n",
            "Iteration:72\n",
            "MSE Loss is:4.013012961155881\n",
            "Iteration:73\n",
            "MSE Loss is:4.011931198771516\n",
            "Iteration:74\n",
            "MSE Loss is:4.010853974521381\n",
            "Iteration:75\n",
            "MSE Loss is:4.009781269367422\n",
            "Iteration:76\n",
            "MSE Loss is:4.008713064351424\n",
            "Iteration:77\n",
            "MSE Loss is:4.007649340594759\n",
            "Iteration:78\n",
            "MSE Loss is:4.006590079297919\n",
            "Iteration:79\n",
            "MSE Loss is:4.005535261740319\n",
            "Iteration:80\n",
            "MSE Loss is:4.004484869279898\n",
            "Iteration:81\n",
            "MSE Loss is:4.003438883352788\n",
            "Iteration:82\n",
            "MSE Loss is:4.002397285473006\n",
            "Iteration:83\n",
            "MSE Loss is:4.001360057232138\n",
            "Iteration:84\n",
            "MSE Loss is:4.000327180298961\n",
            "Iteration:85\n",
            "MSE Loss is:3.9992986364191823\n",
            "Iteration:86\n",
            "MSE Loss is:3.9982744074150762\n",
            "Iteration:87\n",
            "MSE Loss is:3.9972544751851737\n",
            "Iteration:88\n",
            "MSE Loss is:3.9962388217039475\n",
            "Iteration:89\n",
            "MSE Loss is:3.9952274290214898\n",
            "Iteration:90\n",
            "MSE Loss is:3.9942202792631933\n",
            "Iteration:91\n",
            "MSE Loss is:3.9932173546294365\n",
            "Iteration:92\n",
            "MSE Loss is:3.992218637395281\n",
            "Iteration:93\n",
            "MSE Loss is:3.9912241099101253\n",
            "Iteration:94\n",
            "MSE Loss is:3.9902337545974316\n",
            "Iteration:95\n",
            "MSE Loss is:3.989247553954386\n",
            "Iteration:96\n",
            "MSE Loss is:3.988265490551615\n",
            "Iteration:97\n",
            "MSE Loss is:3.9872875470328517\n",
            "Iteration:98\n",
            "MSE Loss is:3.98631370611465\n",
            "Iteration:99\n",
            "[11.601515607782211, 21.90827284512612, 32.21503008247003, 42.52178731981394, 52.82854455715785, 63.13530179450176, 73.44205903184567, 83.74881626918958, 94.05557350653348, 104.3623307438774]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# predict new value corresponds to input\n",
        "xnew = [2.5, 7.1, 5.78, 3.54, 8.02]\n",
        "ynew = linear_pred(xnew)\n",
        "print(ynew)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "jW6rcVsbNe4E",
        "outputId": "cbd812fc-af28-4f1e-c13b-4116b9f66804"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[27.063049115161, 74.47269147188602, 60.868185404304064, 37.78175086537709, 83.95461994323102]\n"
          ]
        }
      ]
    }
  ]
}